<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="vue.js"></script>
  </head>
  <body>
    <div id="root">
      <child content="aa"></child>
      <child content="bb"></child>
    </div>

    <script>
      //Vue原型设置总线对象
      Vue.prototype.bus = new Vue();

      Vue.component('child', {
        props: {
          content: String
        },
        //克隆外部组件传入的值
        data() {
          return {
            mycontent: this.content
          };
        },
        template: '<div @click="handleClick">{{mycontent}}</div>',
        methods: {
          handleClick: function() {
            //总线对象触发change事件
            this.bus.$emit('change', this.mycontent);
          }
        },
        //页面元素渲染之后会执行mounted
        mounted() {
          var this_ = this;
          //监听change事件 触发后执行function
          this.bus.$on('change', function(msg) {
            this_.mycontent = msg;
          });
        }
      });

      new Vue({
        el: '#root'
      });
    </script>
    <!-- 非父子组件传值的方式有两种
            1.借助vue官方提供的数据层的框架vuex
            2.使用总线机制--观察者模式（本次案例时候总线机制）
      -->
  </body>
</html>
